package com.kuangstudy.config.mvc;

import com.kuangstudy.handler.LogInterceptor;
import com.kuangstudy.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * Author: yykk Administrator
 * Version: 1.0
 * Create Date Time: 2021/11/15 21:49.
 * Update Date Time:
 *
 * @see
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${file.staticPatternPath}")
    private String staticPatternPath;
    @Value("${file.uploadFolder}")
    private String uploadFolder;

    /**
     * @author zhangfushuai
     * @description （本地文件上传）静态资源路径映射 - 其实就自定义静态资源服务器
     * @since 2022/4/17 18:17
     * 参数 registry
     * @return void
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticPatternPath).addResourceLocations("file:" + uploadFolder);
    }


    /**
     * @author zhangfushuai
     * @description 把LoginInterceptor拦截器放入到ioc容器中
     * @since 2022/4/17 18:19
     * 参数
     * @return com.kuangstudy.handler.LoginInterceptor
     **/
    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public LogInterceptor getLogInterceptor() {
        return new LogInterceptor();
    }


    /**
     * @author zhangfushuai
     * @description 注册拦截器的方法
     * @since 2022/4/17 18:19
     * 参数 registry
     * @return void
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // registry.addInterceptor(getLoginInterceptor()) 注册拦截器
        registry.addInterceptor(getLoginInterceptor())
                // 拦截器进入的路由规则是什么，addPathPatterns 代表以下配置的路由都会进行LoginInterceptor
                // 求执行一次在进入路由springmvc的具体方法中
                .addPathPatterns("/detail/**", "/", "/index", "/api/**", "/blog/**","/acv")
                // 代表不拦截路由
                .excludePathPatterns("/detail/new/**");


        registry.addInterceptor(getLogInterceptor())
                // 拦截器进入的路由规则是什么，addPathPatterns 代表以下配置的路由都会进行LoginInterceptor
                // 求执行一次在进入路由springmvc的具体方法中
                .addPathPatterns("/detail/**", "/", "/index")
                // 代表不拦截路由
                .excludePathPatterns("/detail/new/**");
    }

}
